package com.lx.service;

import com.lx.dao.UserDao;
import com.lx.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service  //将当前类对象自动注入到Spring容器中
public class UserService { //调用userDao中的方法，处理业务逻辑
    @Autowired  //将spring容器中的bean自动注入到需要的位置
    private UserDao userDao;

    /**
     * 用户登录
     *
     * @param user_name 用户名
     * @param password  密码
     * @return 成功返回 user 失败返回 null
     */
    public User login(String user_name, String password) {
        User user = userDao.login(user_name, password);
        return user;
    }

    /**
     * 用户注册
     * @param user_name
     * @param password
     * @param real_name
     * @param birthday
     * @param phone
     * @param address
     * @return
     */
    public User registerUser(String user_name, String password, String real_name
            , String birthday, String phone, String address){
        //判断用户是否存在
        User user = userDao.getIsUser(user_name);
        if (user == null){
            //完成注册
            boolean register = userDao.registerUser(user_name, password, real_name, birthday, phone, address);
            //验证是否注册成功
            System.out.println(register);
            return userDao.getIsUser(user_name);
        }else {
            System.out.println("该账户已存在！");
            return null;
        }
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    public boolean updateUser(User user){
        boolean b = userDao.updateUser(user);
        return b;
    }
}
